package br.com.fafica.fast_restaurant.view;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import java.awt.Color;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.SwingConstants;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;

import br.com.fafica.fast_restaurant.control.ControlCliente;
import br.com.fafica.fast_restaurant.model.dao.FabricaConecxao;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;

public class TelaEditarCliente extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField textFieldNome;
	private JTextField textFieldEstado;
	private JTextField textFieldEnd;
	private JTextField textFieldBairro;
	private JTextField textFieldCpf;
	private JTextField textFieldCidade;
	private JTextField textFieldCep;
	private JTextField textFieldFone;

	/**
	 * Create the dialog.
	 * 
	 * 
	 */


	public TelaEditarCliente(java.awt.Frame parent, boolean moda1,
			String Nome, String End, String Bairro, String Cidade,
			String Estado, String Cep, String Cpf, String Fone) {

		super(parent, moda1);

		final TelaEditarCliente estaTela = this;
		setBounds(100, 100, 794, 414);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBackground(new Color(0, 102, 0));
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);

		JPanel panel = new JPanel();
		panel.setBorder(new EmptyBorder(5, 5, 5, 5));
		panel.setBackground(new Color(0, 100, 0));

		JLabel label = new JLabel("CADASTRO CLIENTE");
		label.setVerticalAlignment(SwingConstants.BOTTOM);
		label.setForeground(new Color(255, 215, 0));
		label.setFont(new Font("Arial Black", Font.PLAIN, 20));

		JLabel label_1 = new JLabel();
		label_1.setText("NOME:");
		label_1.setForeground(new Color(255, 215, 0));
		label_1.setFont(new Font("Tahoma", Font.BOLD, 12));
		label_1.setBackground(Color.GRAY);

		textFieldNome = new JTextField();
		this.textFieldNome.setText(Nome);

		JLabel label_2 = new JLabel();
		label_2.setText("ESTADO:");
		label_2.setForeground(new Color(255, 215, 0));
		label_2.setFont(new Font("Tahoma", Font.BOLD, 12));
		label_2.setBackground(Color.GRAY);

		textFieldEstado = new JTextField();
		textFieldEstado.setText(Estado);

		JLabel label_3 = new JLabel();
		label_3.setText("ENDERE\u00C7O:");
		label_3.setForeground(new Color(255, 215, 0));
		label_3.setFont(new Font("Tahoma", Font.BOLD, 12));
		label_3.setBackground(Color.GRAY);

		textFieldEnd = new JTextField();
		textFieldEnd.setText(End);

		JLabel label_4 = new JLabel();
		label_4.setText("CPF:");
		label_4.setForeground(new Color(255, 215, 0));
		label_4.setFont(new Font("Tahoma", Font.BOLD, 12));
		label_4.setBackground(Color.GRAY);

		JLabel label_5 = new JLabel();
		label_5.setText("BAIRRO:");
		label_5.setForeground(new Color(255, 215, 0));
		label_5.setFont(new Font("Tahoma", Font.BOLD, 12));
		label_5.setBackground(Color.GRAY);

		textFieldBairro = new JTextField();
		textFieldBairro.setText(Bairro);

		JLabel label_6 = new JLabel();
		label_6.setText("CIDADE:");
		label_6.setForeground(new Color(255, 215, 0));
		label_6.setFont(new Font("Tahoma", Font.BOLD, 12));
		label_6.setBackground(Color.GRAY);

		textFieldCpf = new JTextField();
		textFieldCpf.setEditable(false);
		textFieldCpf.setText(Cpf);

		textFieldCidade = new JTextField();
		textFieldCidade.setText(Cidade);

		JLabel label_7 = new JLabel();
		label_7.setText("FONE:");
		label_7.setForeground(new Color(255, 215, 0));
		label_7.setFont(new Font("Tahoma", Font.BOLD, 12));
		label_7.setBackground(Color.GRAY);

		JLabel label_8 = new JLabel();
		label_8.setText("CEP:");
		label_8.setForeground(new Color(255, 215, 0));
		label_8.setFont(new Font("Tahoma", Font.BOLD, 12));
		label_8.setBackground(Color.GRAY);

		textFieldCep = new JTextField();
		textFieldCep.setText(Cep);

		textFieldFone = new JTextField();
		textFieldFone.setText(Fone);

		JButton button = new JButton("SALVAR");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {

				//
				// ArrayList<String> Registro = new ArrayList<String>();
				// Registro.add(textFieldNome.getText());
				// Registro.add(textFieldEnd.getText());
				// Registro.add(textFieldBairro.getText());
				// Registro.add(textFieldCidade.getText());
				// Registro.add(textFieldEstado.getText());
				// Registro.add(textFieldCep.getText());
				// Registro.add(textFieldFone.getText());
				// Registro.add(textFieldCpf.getText());
				//
				// ControlCliente ControllerCliente = new ControlCliente();
				// ControllerCliente.Atualizar(Registro);
				// }
				try {
					
					Connection conn = FabricaConecxao.GeraConexao();
					String query = "update cad_cliente set nome = ?, end = ?, bairro = ?, cidade = ?,"
							+ "estado = ?, cep = ?, fone = ? where cpf = ?";
					PreparedStatement cmd = conn.prepareStatement(query);

					cmd.setString(1, textFieldNome.getText());
					cmd.setString(2, textFieldEnd.getText());
					cmd.setString(3, textFieldBairro.getText());
					cmd.setString(4, textFieldCidade.getText());
					cmd.setString(5, textFieldEstado.getText());
					cmd.setString(6, textFieldCep.getText());
					cmd.setString(7, textFieldFone.getText());
					cmd.setString(8, textFieldCpf.getText());

					cmd.executeUpdate();
					cmd.close();
					
					JOptionPane.showMessageDialog(null, "Dados Atualizados Com Sucesso!!! ");
					estaTela.dispose();
				} catch (SQLException ex) {
					System.out.println("Ocorreu um erro sql" + ex.getMessage());
					JOptionPane.showMessageDialog(null, "Erro ao Atualizar os Dados");
				}
				
				
				
			}

		});

		JButton button_1 = new JButton("SAIR");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				estaTela.dispose();
			}
		});
		button_1.setOpaque(false);
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(gl_panel
				.createParallelGroup(Alignment.LEADING)
				.addGap(0, 779, Short.MAX_VALUE)
				.addGroup(
						gl_panel.createSequentialGroup()
								.addGroup(
										gl_panel.createParallelGroup(
												Alignment.LEADING)
												.addGroup(
														gl_panel.createSequentialGroup()
																.addGap(270)
																.addComponent(
																		label,
																		GroupLayout.PREFERRED_SIZE,
																		259,
																		GroupLayout.PREFERRED_SIZE))
												.addGroup(
														gl_panel.createSequentialGroup()
																.addGap(60)
																.addComponent(
																		label_1,
																		GroupLayout.PREFERRED_SIZE,
																		45,
																		GroupLayout.PREFERRED_SIZE)
																.addGap(29)
																.addComponent(
																		textFieldNome,
																		GroupLayout.PREFERRED_SIZE,
																		596,
																		GroupLayout.PREFERRED_SIZE))
												.addGroup(
														gl_panel.createSequentialGroup()
																.addGap(60)
																.addComponent(
																		label_2,
																		GroupLayout.PREFERRED_SIZE,
																		59,
																		GroupLayout.PREFERRED_SIZE)
																.addGap(15)
																.addComponent(
																		textFieldEstado,
																		GroupLayout.PREFERRED_SIZE,
																		161,
																		GroupLayout.PREFERRED_SIZE))
												.addGroup(
														gl_panel.createSequentialGroup()
																.addGap(60)
																.addGroup(
																		gl_panel.createParallelGroup(
																				Alignment.LEADING,
																				false)
																				.addGroup(
																						gl_panel.createSequentialGroup()
																								.addComponent(
																										label_3,
																										GroupLayout.PREFERRED_SIZE,
																										71,
																										GroupLayout.PREFERRED_SIZE)
																								.addGap(3)
																								.addComponent(
																										textFieldEnd,
																										GroupLayout.PREFERRED_SIZE,
																										596,
																										GroupLayout.PREFERRED_SIZE))
																				.addGroup(
																						gl_panel.createSequentialGroup()
																								.addGroup(
																										gl_panel.createParallelGroup(
																												Alignment.TRAILING)
																												.addComponent(
																														label_4,
																														GroupLayout.PREFERRED_SIZE,
																														35,
																														GroupLayout.PREFERRED_SIZE)
																												.addGroup(
																														gl_panel.createSequentialGroup()
																																.addComponent(
																																		label_5,
																																		GroupLayout.PREFERRED_SIZE,
																																		71,
																																		GroupLayout.PREFERRED_SIZE)
																																.addGap(3)
																																.addComponent(
																																		textFieldBairro,
																																		GroupLayout.PREFERRED_SIZE,
																																		161,
																																		GroupLayout.PREFERRED_SIZE)
																																.addGap(26)
																																.addComponent(
																																		label_6,
																																		GroupLayout.PREFERRED_SIZE,
																																		52,
																																		GroupLayout.PREFERRED_SIZE)))
																								.addGap(10)
																								.addGroup(
																										gl_panel.createParallelGroup(
																												Alignment.LEADING,
																												false)
																												.addComponent(
																														textFieldCpf)
																												.addComponent(
																														textFieldCidade,
																														GroupLayout.DEFAULT_SIZE,
																														137,
																														Short.MAX_VALUE))
																								.addGap(18)
																								.addGroup(
																										gl_panel.createParallelGroup(
																												Alignment.LEADING)
																												.addComponent(
																														label_7,
																														GroupLayout.PREFERRED_SIZE,
																														45,
																														GroupLayout.PREFERRED_SIZE)
																												.addComponent(
																														label_8,
																														GroupLayout.PREFERRED_SIZE,
																														35,
																														GroupLayout.PREFERRED_SIZE))
																								.addPreferredGap(
																										ComponentPlacement.RELATED)
																								.addGroup(
																										gl_panel.createParallelGroup(
																												Alignment.LEADING)
																												.addComponent(
																														textFieldCep,
																														GroupLayout.DEFAULT_SIZE,
																														143,
																														Short.MAX_VALUE)
																												.addComponent(
																														textFieldFone,
																														GroupLayout.DEFAULT_SIZE,
																														143,
																														Short.MAX_VALUE)))))
												.addGroup(
														gl_panel.createSequentialGroup()
																.addGap(303)
																.addComponent(
																		button)
																.addGap(18)
																.addComponent(
																		button_1,
																		GroupLayout.PREFERRED_SIZE,
																		77,
																		GroupLayout.PREFERRED_SIZE)))
								.addContainerGap(39, Short.MAX_VALUE)));
		gl_panel.setVerticalGroup(gl_panel
				.createParallelGroup(Alignment.LEADING)
				.addGap(0, 377, Short.MAX_VALUE)
				.addGroup(
						gl_panel.createSequentialGroup()
								.addGap(36)
								.addComponent(label,
										GroupLayout.PREFERRED_SIZE, 43,
										GroupLayout.PREFERRED_SIZE)
								.addGap(43)
								.addGroup(
										gl_panel.createParallelGroup(
												Alignment.LEADING)
												.addComponent(
														label_1,
														GroupLayout.PREFERRED_SIZE,
														20,
														GroupLayout.PREFERRED_SIZE)
												.addGroup(
														gl_panel.createSequentialGroup()
																.addGap(1)
																.addComponent(
																		textFieldNome,
																		GroupLayout.PREFERRED_SIZE,
																		GroupLayout.DEFAULT_SIZE,
																		GroupLayout.PREFERRED_SIZE)))
								.addGap(10)
								.addGroup(
										gl_panel.createParallelGroup(
												Alignment.LEADING)
												.addComponent(
														label_3,
														GroupLayout.PREFERRED_SIZE,
														20,
														GroupLayout.PREFERRED_SIZE)
												.addGroup(
														gl_panel.createSequentialGroup()
																.addGap(1)
																.addComponent(
																		textFieldEnd,
																		GroupLayout.PREFERRED_SIZE,
																		GroupLayout.DEFAULT_SIZE,
																		GroupLayout.PREFERRED_SIZE)))
								.addGroup(
										gl_panel.createParallelGroup(
												Alignment.LEADING)
												.addGroup(
														gl_panel.createSequentialGroup()
																.addGap(10)
																.addGroup(
																		gl_panel.createParallelGroup(
																				Alignment.LEADING)
																				.addComponent(
																						label_5,
																						GroupLayout.PREFERRED_SIZE,
																						20,
																						GroupLayout.PREFERRED_SIZE)
																				.addGroup(
																						gl_panel.createSequentialGroup()
																								.addGap(1)
																								.addComponent(
																										textFieldBairro,
																										GroupLayout.PREFERRED_SIZE,
																										GroupLayout.DEFAULT_SIZE,
																										GroupLayout.PREFERRED_SIZE))
																				.addComponent(
																						label_6,
																						GroupLayout.PREFERRED_SIZE,
																						20,
																						GroupLayout.PREFERRED_SIZE)))
												.addGroup(
														gl_panel.createSequentialGroup()
																.addGap(11)
																.addGroup(
																		gl_panel.createParallelGroup(
																				Alignment.BASELINE)
																				.addComponent(
																						textFieldCidade,
																						GroupLayout.PREFERRED_SIZE,
																						GroupLayout.DEFAULT_SIZE,
																						GroupLayout.PREFERRED_SIZE)
																				.addComponent(
																						label_8,
																						GroupLayout.PREFERRED_SIZE,
																						20,
																						GroupLayout.PREFERRED_SIZE)))
												.addGroup(
														gl_panel.createSequentialGroup()
																.addPreferredGap(
																		ComponentPlacement.UNRELATED)
																.addComponent(
																		textFieldCep,
																		GroupLayout.PREFERRED_SIZE,
																		GroupLayout.DEFAULT_SIZE,
																		GroupLayout.PREFERRED_SIZE)))
								.addGroup(
										gl_panel.createParallelGroup(
												Alignment.LEADING)
												.addGroup(
														gl_panel.createSequentialGroup()
																.addGap(10)
																.addGroup(
																		gl_panel.createParallelGroup(
																				Alignment.LEADING)
																				.addComponent(
																						label_2,
																						GroupLayout.PREFERRED_SIZE,
																						20,
																						GroupLayout.PREFERRED_SIZE)
																				.addGroup(
																						gl_panel.createSequentialGroup()
																								.addGap(1)
																								.addComponent(
																										textFieldEstado,
																										GroupLayout.PREFERRED_SIZE,
																										GroupLayout.DEFAULT_SIZE,
																										GroupLayout.PREFERRED_SIZE))))
												.addGroup(
														gl_panel.createSequentialGroup()
																.addPreferredGap(
																		ComponentPlacement.UNRELATED)
																.addGroup(
																		gl_panel.createParallelGroup(
																				Alignment.BASELINE)
																				.addComponent(
																						label_4,
																						GroupLayout.PREFERRED_SIZE,
																						20,
																						GroupLayout.PREFERRED_SIZE)
																				.addComponent(
																						textFieldCpf,
																						GroupLayout.PREFERRED_SIZE,
																						GroupLayout.DEFAULT_SIZE,
																						GroupLayout.PREFERRED_SIZE)
																				.addComponent(
																						label_7,
																						GroupLayout.PREFERRED_SIZE,
																						20,
																						GroupLayout.PREFERRED_SIZE)
																				.addComponent(
																						textFieldFone,
																						GroupLayout.PREFERRED_SIZE,
																						GroupLayout.DEFAULT_SIZE,
																						GroupLayout.PREFERRED_SIZE))))
								.addGap(68)
								.addGroup(
										gl_panel.createParallelGroup(
												Alignment.LEADING)
												.addComponent(
														button,
														GroupLayout.PREFERRED_SIZE,
														38,
														GroupLayout.PREFERRED_SIZE)
												.addComponent(
														button_1,
														GroupLayout.PREFERRED_SIZE,
														38,
														GroupLayout.PREFERRED_SIZE))
								.addGap(73)));
		panel.setLayout(gl_panel);
		GroupLayout gl_contentPanel = new GroupLayout(contentPanel);
		gl_contentPanel.setHorizontalGroup(gl_contentPanel.createParallelGroup(
				Alignment.LEADING).addGroup(
				gl_contentPanel
						.createSequentialGroup()
						.addComponent(panel, GroupLayout.PREFERRED_SIZE, 779,
								GroupLayout.PREFERRED_SIZE)
						.addContainerGap(123, Short.MAX_VALUE)));
		gl_contentPanel.setVerticalGroup(gl_contentPanel.createParallelGroup(
				Alignment.LEADING).addGroup(
				gl_contentPanel
						.createSequentialGroup()
						.addComponent(panel, GroupLayout.PREFERRED_SIZE, 377,
								GroupLayout.PREFERRED_SIZE)
						.addContainerGap(145, Short.MAX_VALUE)));
		contentPanel.setLayout(gl_contentPanel);
	}

}
